package Util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ExportSqlUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExportSqlUtil.class);

    //	public static void main(String[] args) {
//		try {
////			runCMD();
//			backup("root","950906","station");
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
    public static boolean backup(String username, String password, String database) {
        LOGGER.info("begin -  backup ");
        boolean bool = false;
        String sqlFilename = database + "_" + new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()) + ".sql";
        String cmd = "mysqldump -u " + username + " -p" + password + " --opt " + database + " > d:/" + sqlFilename;
        try {
            Process p = Runtime.getRuntime().exec("cmd /C" + cmd);
            p.waitFor();
            bool = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bool;
    }

    public static boolean truncate_table() {
        String sql1 = "truncate table refund;";
        String sql2 = "truncate table pay;";
        boolean ac;
        try {
            //建立连接
            Connection conn = JdbcUtil.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql1);
            //执行语句
            pstmt.executeUpdate();
            JdbcUtil.close(pstmt, conn);
            ac = true;
        } catch (SQLException e) {
            e.printStackTrace();
            ac = false;
        }
        return ac;
    }
}